<template>
    <el-drawer
    :title="!dataForm.id ? '新增' : '修改'"
    direction="rtl"
    ref="drawer"
    size="40%"
    :visible.sync="visible">
    <div class="demo-drawer__header">
      <el-button size="mini" @click="visible = false">取 消</el-button>
      <el-button type="primary" plain size="mini" @click="dataFormSubmit()">确定</el-button>
    </div>
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
      <el-form-item label="租户名称" prop="tenantName">
        <el-input v-model="dataForm.tenantName" placeholder="租户名称"></el-input>
      </el-form-item>
      <el-form-item label="社会信用代码" prop="certificateCode">
        <el-input v-model="dataForm.certificateCode" placeholder="社会信用统一代码"></el-input>
      </el-form-item>
      <el-form-item label="租户地址" prop="address">
        <el-input v-model="dataForm.address" placeholder="租户地址"></el-input>
      </el-form-item>
      <el-form-item label="企业法人" prop="businessEntity">
        <el-input v-model="dataForm.businessEntity" placeholder="企业法人"></el-input>
      </el-form-item>
      <el-form-item label="法人手机号" prop="businessEntityMobile">
        <el-input v-model="dataForm.businessEntityMobile" placeholder="法人手机号"></el-input>
      </el-form-item>
      <el-form-item label="联系人" prop="contactPerson">
        <el-input v-model="dataForm.contactPerson" placeholder="联系人"></el-input>
      </el-form-item>
      <el-form-item label="联系人手机号" prop="contactPersonMobile">
        <el-input v-model="dataForm.contactPersonMobile" placeholder="联系人手机号"></el-input>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="dataForm.status" placeholder="请选择状态" style="width:100%">
          <el-option
            v-for="item in statuses"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>
  </el-drawer>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          tenantName: '',
          address: '',
          businessEntity: '',
          businessEntityMobile: '',
          contactPerson: '',
          contactPersonMobile: '',
          certificateCode: '',
          status: ''
        },
        statuses:[
          {label:'无效', value:0},
          {label:'有效', value:1}
        ],
        dataRule: {
          tenantName: [
            { required: true, message: '租户名称不能为空', trigger: 'blur' }
          ],
          address: [
            { required: true, message: '租户地址不能为空', trigger: 'blur' }
          ],
          businessEntity: [
            { required: true, message: '企业法人不能为空', trigger: 'blur' }
          ],
          businessEntityMobile: [
            { required: true, message: '法人手机号不能为空', trigger: 'blur' }
          ],
          contactPerson: [
            { required: true, message: '联系人不能为空', trigger: 'blur' }
          ],
          contactPersonMobile: [
            { required: true, message: '联系人手机号不能为空', trigger: 'blur' }
          ],
          certificateCode: [
            { required: true, message: '社会信用统一代码不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '状态', trigger: 'blur' }
          ],
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id > 0) {
            this.$http({
              url: this.$http.adornUrl(`/tenant/tenantInfo/justInfo/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.tenantName = data.t.tenantName
                this.dataForm.address = data.t.address
                this.dataForm.businessEntity = data.t.businessEntity
                this.dataForm.businessEntityMobile = data.t.businessEntityMobile
                this.dataForm.contactPerson = data.t.contactPerson
                this.dataForm.contactPersonMobile = data.t.contactPersonMobile
                this.dataForm.certificateCode = data.t.certificateCode
                this.dataForm.status = data.t.status
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/tenant/tenantInfo/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'tenantId': this.dataForm.id || undefined,
                'tenantName': this.dataForm.tenantName,
                'address': this.dataForm.address,
                'businessEntity': this.dataForm.businessEntity,
                'businessEntityMobile': this.dataForm.businessEntityMobile,
                'contactPerson': this.dataForm.contactPerson,
                'contactPersonMobile': this.dataForm.contactPersonMobile,
                'certificateCode': this.dataForm.certificateCode,
                'status': this.dataForm.status
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>

<style lang="scss">
    .demo-drawer {
        &__content {
            display: flex;
            flex-direction: column;
            height: 100%;
            form {
                flex: 1;
            }
        }
        &__footer {
            display: flex;
            button {
                flex: 1;
            }
        }
        &__header {
            text-align:right;
            margin-bottom:20px;
            width:100%;
        }
    }

    .el-drawer__body {
        padding: 20px;
    }

</style>
